package com.aerozhonghuan.logisticsmall.modules.mine.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aerozhonghuan.logisticsmall.R;
import com.aerozhonghuan.logisticsmall.framework.base.Constants;
import com.aerozhonghuan.logisticsmall.modules.mine.beans.MessageListBean;
import com.aerozhonghuan.logisticsmall.modules.order.OrderDetailAcrivity;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述
 *
 * @author zhangyonghui
 * 创建日期 2019-05-23
 */
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MyViewHolder> {

    private Context context;
    private List<MessageListBean.MessageBean> messageList;

    public MessageAdapter(Context context) {
        this.context = context;
        messageList = new ArrayList<>();
    }

    public void addList(List<MessageListBean.MessageBean> messageList) {
        this.messageList.addAll(messageList);
        notifyDataSetChanged();
    }

    public void refreshList(List<MessageListBean.MessageBean> messageList) {
        this.messageList.clear();
        this.messageList.addAll(messageList);
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_me_message, null);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        MessageListBean.MessageBean messageBean = messageList.get(position);
        if (messageBean == null) {
            return;
        }
        holder.tvTitle.setText(messageBean.getTitle());
        holder.tvTime.setText(messageBean.getCreatetime());
        holder.tvContent.setText(messageBean.getText());
        if (messageBean.getMsgType() == Constants.TYPE_MESSAGE_ORDERDETAIL) {
            try {
                String attrs = messageBean.getAttrs().toString();
                JSONObject jsonObject = new JSONObject(attrs);
                String btnText = jsonObject.optString("btnText");
                final String billId = jsonObject.optString("billId");
                holder.relBtn.setVisibility(View.VISIBLE);
                holder.btnMore.setText(btnText);
                holder.btnMore.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        context.startActivity(new Intent(context, OrderDetailAcrivity.class).putExtra("billId", billId));
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            holder.relBtn.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return messageList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView tvTitle;
        public TextView tvTime;
        public TextView tvContent;
        public RelativeLayout relBtn;
        public Button btnMore;

        public MyViewHolder(View itemView) {
            super(itemView);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvTime = (TextView) itemView.findViewById(R.id.tv_time);
            tvContent = (TextView) itemView.findViewById(R.id.tv_content);
            relBtn = (RelativeLayout) itemView.findViewById(R.id.rel_btn);
            btnMore = (Button) itemView.findViewById(R.id.btn_more);
        }
    }

}
